package org.jeecg.modules.LdProjectInfo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.LdProjectInfo.entity.BaoZhuang;
import org.jeecg.modules.LdProjectInfo.entity.DgljCost;
import org.jeecg.modules.LdProjectInfo.entity.JgCost;
import org.jeecg.modules.LdProjectInfo.entity.LdProjectInfo;
import org.jeecg.modules.LdProjectInfo.mapper.LdProjectInfoMapper;
import org.jeecg.modules.LdProjectInfo.service.ILdProjectInfoService;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.deviceInfo.entity.DeviceInfo;
import org.jeecg.modules.deviceInfo.service.IDeviceInfoService;
import org.jeecg.modules.zcgl.entity.AssetFixed;
import org.jeecg.modules.zcgl.entity.AssetFixedPutImport;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: 报价基本情况
 * @Author: jeecg-boot
 * @Date:   2025-05-06
 * @Version: V1.0
 */
@Service
public class LdProjectInfoServiceImpl extends ServiceImpl<LdProjectInfoMapper, LdProjectInfo> implements ILdProjectInfoService {
    @Autowired
    private IDeviceInfoService deviceInfoService;
    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @Override
    @Transactional
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response, Class<JgCost> clazz) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<JgCost> newList = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
                Set<String> nameSpecSet = new HashSet<>();
                for (JgCost item : newList) {
                    // 生成名称和规格的组合键
                    String key = item.getDeviceName();
                    // 检查是否已经存在
//                    if (nameSpecSet.contains(key)) {
//                        throw new Exception("发现重复数据: 名称 = " + item.getDeviceName());
//                    }
                    DeviceInfo deviceInfo = deviceInfoService.getOne(new QueryWrapper<DeviceInfo>().eq("device_name_model",item.getDeviceName()));
                   if(deviceInfo==null){
                       if (nameSpecSet.contains(key)) {
                           throw new Exception("未获取到设备费率: 名称 = " + item.getDeviceName());
                       }

                   }else{

                       item.setDeviceInfoValue(deviceInfo);
                   }






                    // 将组合键添加到 Set 中
                    nameSpecSet.add(key);
                }


                System.out.println(newList + "文档内容");
                int add = 0;
                int edit = 0;
                List resultList = new ArrayList();

                long start = System.currentTimeMillis();

//                resultList   这是返回数据

                System.out.println("查看全部数据" + newList);
                return Result.ok(newList);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  null;
    }

    @Override
    public Result<?> importExcelBz(HttpServletRequest request, HttpServletResponse response, Class<BaoZhuang> clazz) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<BaoZhuang> newList = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
                Set<String> nameSpecSet = new HashSet<>();

                System.out.println(newList + "文档内容");
                int add = 0;
                int edit = 0;
                List resultList = new ArrayList();

                long start = System.currentTimeMillis();

//                resultList   这是返回数据

                System.out.println("查看全部数据" + newList);
                return Result.ok(newList);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  null;
    }


    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @Override
    @Transactional
    public Result<?> importExcelgdlj(HttpServletRequest request, HttpServletResponse response, Class<DgljCost> clazz) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<DgljCost> newList = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
                Set<String> nameSpecSet = new HashSet<>();

                System.out.println(newList + "文档内容");
                int add = 0;
                int edit = 0;
                List resultList = new ArrayList();

                long start = System.currentTimeMillis();

//                resultList   这是返回数据

                System.out.println("查看全部数据" + newList);
                return Result.ok(newList);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  null;
    }
}
